package org.jeecg.modules.yc.web.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yc.web.entity.Terminal;
import org.jeecg.modules.yc.web.service.IPdDeviceService;
import org.jeecg.modules.yc.web.service.ITerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: 采集器
 * @Author: jeecg-boot
 * @Date:   2023-12-25
 * @Version: V1.0
 */
@Api(tags="采集器")
@RestController
@RequestMapping("/yc/web/terminal")
@Slf4j
public class TerminalController extends JeecgController<Terminal, ITerminalService> {
	@Autowired
	private ITerminalService terminalService;

	@Autowired
	private IPdDeviceService pdDeviceService;
	
	/**
	 * 分页列表查询
	 *
	 * @param terminal
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return yc/terminal/TerminalList
	 */
	//@AutoLog(value = "采集器-分页列表查询")
	@ApiOperation(value="采集器-分页列表查询", notes="采集器-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="yc/terminal/TerminalList")
	public Result<IPage<Terminal>> queryPageList(Terminal terminal,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												 HttpServletRequest req) {
		QueryWrapper<Terminal> queryWrapper = QueryGenerator.initQueryWrapper(terminal, req.getParameterMap());
		Page<Terminal> page = new Page<Terminal>(pageNo, pageSize);
		IPage<Terminal> pageList = terminalService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	@GetMapping(value = "/listWithDeviceCount")
	public Result<IPage<Terminal>> listWithDeviceCount(Terminal terminal,
												@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
												@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
												HttpServletRequest req) {
		QueryWrapper<Terminal> queryWrapper = QueryGenerator.initQueryWrapper(terminal, req.getParameterMap());
		Page<Terminal> page = new Page<Terminal>(pageNo, pageSize);
		IPage<Terminal> pageList = terminalService.page(page, queryWrapper);
		pageList.getRecords().forEach(v ->{
			v.setDeviceNum(pdDeviceService.deviceCountByTerminalId(v.getId()));
		});
		return Result.OK(pageList);
	}

	/**
	 * 分页列表查询没有设置坐标的设备
	 *
	 * @param terminal
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return yc/terminal/TerminalList
	 */
	//@AutoLog(value = "采集器-分页列表查询")
	@ApiOperation(value="采集器-分页列表查询", notes="采集器-分页列表查询")
	@GetMapping(value = "/listCoordinateIsNull")
	@PermissionData(pageComponent="yc/terminal/queryPageListCoordinateIsNull")
	public Result<IPage<Terminal>> queryPageListCoordinateIsNull(Terminal terminal,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												 HttpServletRequest req, @RequestParam(name="mark")String mark) {
		QueryWrapper<Terminal> queryWrapper = QueryGenerator.initQueryWrapper(terminal, req.getParameterMap());
		if("isNull".equals(mark)){
			queryWrapper.isNull("coordinate");
		}else{
			queryWrapper.isNotNull("coordinate");
		}
		Page<Terminal> page = new Page<Terminal>(pageNo, pageSize);
		IPage<Terminal> pageList = terminalService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	/**
	 *   添加
	 *
	 * @param terminal
	 * @return
	 */
	@AutoLog(value = "采集器-添加")
	@ApiOperation(value="采集器-添加", notes="采集器-添加")
	@RequiresPermissions("/yc/web:terminal:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Terminal terminal) {
		System.out.println(JSON.toJSONString(terminal));
		terminalService.save(terminal);
		System.out.println("添加后进行Id看看"+terminal);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param terminal
	 * @return
	 */
	@AutoLog(value = "采集器-编辑同步NB")
	@ApiOperation(value="采集器-编辑同步NB", notes="采集器-编辑同步NB")
	@RequiresPermissions("/yc/web:terminal:edit")
	@RequestMapping(value = "/edit_NB", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit_NB(@RequestBody Terminal terminal) {
		terminalService.edit_NB(terminal);
		return Result.OK("编辑成功!");
	}
	@AutoLog(value = "采集器-编辑")
	@ApiOperation(value="采集器-编辑", notes="采集器-编辑")
	@RequiresPermissions("/yc/web:terminal:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Terminal terminal) {
		terminalService.updateById(terminal);
		return Result.OK("编辑成功!");
	}
	@AutoLog(value = "采集器-移除坐标")
	@ApiOperation(value="采集器-移除坐标", notes="采集器-移除坐标")
	/*@RequiresPermissions("/yc/web:terminal:editCoordinateNULL")*/
	@RequestMapping(value = "/editCoordinateNULL", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editCoordinateNULL(@RequestParam(name="id",required=true) String id) {
		System.out.println("设备编号："+id);
		UpdateWrapper<Terminal> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id",id);
		updateWrapper.set("coordinate",null);
		terminalService.update(updateWrapper);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "采集器-通过id删除")
	@ApiOperation(value="采集器-通过id删除", notes="采集器-通过id删除")
	@RequiresPermissions("/yc/web:terminal:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		terminalService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "采集器-批量删除")
	@ApiOperation(value="采集器-批量删除", notes="采集器-批量删除")
	@RequiresPermissions("/yc/web:terminal:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.terminalService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "采集器-通过id查询")
	@ApiOperation(value="采集器-通过id查询", notes="采集器-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Terminal> queryById(@RequestParam(name="id",required=true) String id) {
		Terminal terminal = terminalService.getById(id);
		if(terminal==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(terminal);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param terminal
    */
    @RequiresPermissions("/yc/web:terminal:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Terminal terminal) {
        return super.exportXls(request, terminal, Terminal.class, "采集器");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("/yc/web:terminal:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Terminal.class);
    }
	/**
	 * 生产销售监控模版
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping("/locationMap")
	public ModelAndView location(ModelAndView modelAndView) {
		// 获取登录人用户信息 V2.0版本：
		//获取登录用户信息
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String orgCode=sysUser.getOrgCode();
		modelAndView.setViewName("/map/scatter");
		modelAndView.addObject("abc","12222");
		return modelAndView;
	}
}
